package com.kinghood.productcenter.common.model.dto.product;

import com.kinghood.productcenter.common.model.dao.product.ProductBaseDO;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-01-17 17:43
 **/
@Data
public class UpdateProductLogDTO implements Serializable {

    private Long userId;
    private ProductReqDTO in;
    private ProductDTO oldProductDTO;
    private List<AttributeByProductIdDTO> oldAttributeList;
    private Map<String, AttributeByProductIdDTO> oldProductAttributeMap;
    private Map<String, AttributeByProductIdDTO> oldSkuAttributeMap;
    private String skuLog;
    private  Map<String, Long> oldSkuCodeMap;

    public void handleOldAttribute(List<AttributeByProductIdDTO> oldAttributeList){
        this.oldAttributeList = oldAttributeList;

        Map<String, AttributeByProductIdDTO> productAttributeMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(oldAttributeList)) {
            Map<String, AttributeByProductIdDTO> map = oldAttributeList.stream()
                    .filter(x-> Objects.equals(x.getBusinessType(),0))
                    .collect(Collectors.toMap(x -> x.getAttributeCode() + x.getBusinessType() + x.getBusinessId(), v -> v, (k1, k2) -> k2));
            productAttributeMap.putAll(map);
        }
        this.setOldProductAttributeMap(productAttributeMap);

        Map<String, AttributeByProductIdDTO> skuAttributeMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(oldAttributeList)) {
            Map<String, AttributeByProductIdDTO> map = oldAttributeList.stream()
                    .filter(x -> !Objects.equals(x.getBusinessType(), 0))
                    .collect(Collectors.toMap(x -> x.getAttributeCode() + x.getBusinessType() + x.getBusinessId(), v -> v, (k1, k2) -> k2));
            skuAttributeMap.putAll(map);
        }
        this.setOldSkuAttributeMap(skuAttributeMap);
    }

}
